﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using mshtml;
using System.Collections.Specialized;
using System.Text.RegularExpressions;

namespace SSOLoginApi
{
    /// <summary>
    /// 单点登录组件基类
    /// </summary>
    public class SSOLoginBase
    {

        private string _UserAccount = "";

        /// <summary>
        /// 用户账号
        /// </summary>
        public string UserAccount
        {
            get { return _UserAccount; }
            set { _UserAccount = value; }
        }

        private string _UserPwd = "";

        /// <summary>
        /// 用户密码
        /// </summary>
        public string UserPwd
        {
            get { return _UserPwd; }
            set { _UserPwd = value; }
        }

        /// <summary>
        /// 根据元素ID或NAME属性获取对应的IHTMLElement对象
        /// </summary>
        /// <param name="doc">IHTMLElement对象所在的Document</param>
        /// <param name="id"></param>
        /// <returns></returns>
        protected IHTMLElement getElementById(IHTMLDocument2 doc, string id)
        {
            try
            {
                return (IHTMLElement)doc.all.item(id);
            }
            catch (Exception e)
            {

                throw new Exception(
                    "【根据元素ID或NAME属性获取对应的IHTMLElement对象】失败，");
            }
        }

        /// <summary>
        /// 根据元素ID或NAME属性获取对应的IHTMLElement对象
        /// </summary>
        /// <param name="doc">IHTMLElement对象所在的Document</param>
        /// <param name="id"></param>
        /// <returns></returns>
        protected IHTMLElement getElementByName(IHTMLDocument2 doc, string name, int index)
        {
            try
            {
                return (IHTMLElement)doc.all.item(name, index);
            }
            catch (Exception e)
            {

                throw new Exception(
                    "【根据元素ID或NAME属性获取对应的IHTMLElement对象】失败，");
            }
        }

        /// <summary>
        /// 分析 url 字符串中的参数信息
        /// </summary>
        /// <param name="url">输入的 URL</param>
        /// <param name="baseUrl">输出 URL 的基础部分</param>
        /// <param name="nvc">输出分析后得到的 (参数名,参数值) 的集合</param>
        public NameValueCollection ParseUrl(string url)
        {
            if (url == null)
                throw new ArgumentNullException("url");
            NameValueCollection nvc = new NameValueCollection();

            if (url == "")
                return null;
            int questionMarkIndex = url.IndexOf('?');
            if (questionMarkIndex == -1)
            {
                return null;
            }
            string baseUrl = url.Substring(0, questionMarkIndex);
            if (questionMarkIndex == baseUrl.Length - 1)
                return null;
            string ps = url.Substring(questionMarkIndex + 1);
            // 开始分析参数对  
            Regex re = new Regex(@"(^|&)?(\w+)=([^&]+)(&|$)?", RegexOptions.Compiled);
            MatchCollection mc = re.Matches(ps);
            foreach (Match m in mc)
            {
                nvc.Add(m.Result("$2").ToLower(), m.Result("$3"));
            }

            return nvc;
        }
    }
}
